package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author ludonghuang
 * @since 2021-12-05
 */
@RestController
@Api(description = "课程小节管理")
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    //注入vodClient
    @Autowired
    private VodClient vodClient;

    @ApiOperation(value = "添加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "删除小节")
    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable String id) {
        /* 删除小节的同时，删除阿里云对应的视频 */
        //先根据小节id查询出视频id
        EduVideo video = videoService.getById(id);
        if (!StringUtils.isEmpty(video.getVideoSourceId())) {
            //删除阿里云对应的视频
            R result = vodClient.removeAlyVideo(video.getVideoSourceId());
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除视频失败，熔断器");
            }
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }
}

